package org.sky.logger.support;

import org.sky.logger.mdc.LogMdc;
import org.sky.logger.name.AppNameLoader;
import org.sky.logger.util.Constant;
import org.sky.logger.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import feign.RequestInterceptor;
import feign.RequestTemplate;

public class LogFeignTemplate implements RequestInterceptor {
	private static final Logger logger = LoggerFactory.getLogger(LogFeignTemplate.class);
	private static String appName = AppNameLoader.appName();

	public void apply(RequestTemplate template) {
		try {
			String logid = LogMdc.get(Constant.LogId);
			if (Util.isEmpty(logid)) {
				logid = Util.logId();
			}
			logger.info("LogFeignTemplate appName logid : " + appName + " " + logid);
			template.header(Constant.AppName, appName);
			template.header(Constant.LogId, logid);
		} catch (Exception e) {
			logger.error("LogFeignTemplate apply error", e);
		}
	}

}
